import Vue from "vue";
import VueRouter from "vue-router";
import { getLang } from "@/util/auth";

Vue.use(VueRouter);

const routes = [{
    path: '/',
    name: 'Home',
    component: () => import('@/view/Home')
  },
  {
    path: '/register',
    name: 'register',
    component: () =>
      import('@/view/auth/Register'),
    meta: {
      title: getLang() === 'zh' ? "注册" : 'Sign up'
    }
  },
  {
    path: '/404',
    name: '404',
    component: () =>
      import('@/view/error/404'),
    meta: {
      title: "404 not found"
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () =>
      import('@/view/auth/Login'),
    meta: {
      title: getLang() === 'zh' ? "登录" : 'Sign in'
    }
  },
  {
    path: '/post/create',
    name: 'post-create',
    component: () => import('@/view/post/Create'),
    meta: {
      title: getLang() === 'zh' ? '发布帖子' : 'Create Post',
      requireAuth: true
    }
  },
  {
    name: 'search',
    path: '/search',
    component: () => import('@/view/Search'),
    meta: {title: '检索'}
  },
  {
    name: 'tag',
    path: '/tag',
    component: () => import('@/view/Tag'),
    meta: {title: '检索'}
  },
  {
      path: '*',
      redirect: '/404',
      hidden : true
  },
  {
    path: "/post/:id",
    name: "post-detail",
    component: () => import('@/view/post/Detail'),
  },
  {
    name: 'post-edit',
    path: '/post/edit/:id',
    component: () => import('@/view/post/Edit'),
    meta: {
      title: getLang() === 'zh' ? '编辑帖子' : 'Edit post',
      requireAuth: true
    }
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  },
  //用户主页
  {
    path: '/member/:username/home',
    name: 'user',
    component: () => import('@/view/user/Profile'),
    meta: {
      title: getLang() === 'zh' ? '用户主页' : 'Profile'
    }
  },
  //用户设置
  {
    path: '/member/:username/setting',

    name: 'user-setting',
    component: () => import('@/view/user/Setting'),
    meta: {
      title: getLang() === 'zh' ? '设置' : 'Settings'
    }
  },
  //修改公告
  {
    path: '/billboard/update',
    name: 'billboard',
    component: () => import('@/view/admin/Admin'),
    meta: {
      title: getLang() === 'zh' ? '修改公告' : 'Administration'
    }
  }
]

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
  routes,
});

export default router;